<template>
  <div>
    <!-- 搜索结果页-头部导航 -->
    <div class="search-result-container">
      <!-- 点击实现后退效果 -->
      <van-nav-bar
        title="搜索结果"
        left-arrow
        @click-left="$router.go(-1)"
        fixed
      />
    </div>

    <!-- 文章列表 -->
    <div>
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        :immediate-check="false"
        @load="onLoad"
      >
        <ArticleItem
          v-for="item in articleList"
          :key="item.art_id"
          :artItem="item"
          :isShow="false"
          @click.native="articleDetailClickFn(item.art_id)"
        ></ArticleItem>
      </van-list>
    </div>
  </div>
</template>

<script>
import { searchResultAPI } from "@/api";
import ArticleItem from "@/components/ArticleItem.vue";
export default {
  name: "SearchResult",
  data() {
    return {
      page: 1,
      articleList: [],
      loading: false, //加载状态
      finished: false, //加载全部完成状态
    };
  },
  async created() {
    const res = await searchResultAPI({
      page: this.page,
      q: this.$route.params.kw,
    });

    this.articleList = res.data.data.results;
  },
  components: {
    ArticleItem,
  },
  methods: {
    async onLoad() {
      this.page++;
      const res = await searchResultAPI({
        page: this.page,
        q: this.$route.params.kw,
      });
      if (res.data.data.results.length === 0) {
        return (this.finished = true);
      }
      this.loading = false;
      this.articleList = [...this.articleList, ...res.data.data.results];
    },

    articleDetailClickFn(id) {
      this.$router.push({
        path: `/detail?art_id=${id}`,
      });
    },
  },
};
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
</style>
